package com.developer.t24haber.model;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class NewsParser {

	private final String webAddress;

	public NewsParser(String webAddress) {
		this.webAddress = webAddress;
	}

	public List<News> getNews() throws Exception {
		List<News> news = new ArrayList<News>();
		SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
		XMLReader xmlReader = saxParser.getXMLReader();
		xmlReader.setContentHandler(new NewsXmlHandler(news));
		xmlReader.parse(new InputSource(new StringReader(downloadXmlPage())));
		return news;
	}

	private String downloadXmlPage() throws Exception {
		final String singleQuote = "&amp;#39;";
		final String doubleQuote = "&amp;#34;";
		URL url = new URL(webAddress);
		HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
		urlConnection.addRequestProperty("User-Agent", "Mozilla/4.76");
		InputStream inputStream = urlConnection.getInputStream();
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		StringBuilder stringBuilder = new StringBuilder();
		String line;
		while ((line = bufferedReader.readLine()) != null) {
			stringBuilder.append(line);
		}
		return stringBuilder.toString().replace(singleQuote, "'").replace(doubleQuote, "\"");
	}

	private class NewsXmlHandler extends DefaultHandler {

		private final String ITEM = "item";
		private final String TITLE = "title";
		private final String DESCRIPTION = "description";
		private final String LINK = "link";
		private final String GUID = "guid";
		private final String ENCLOSURE = "enclosure";
		private final String URL = "url";

		private boolean isTitle;
		private boolean isDescription;
		private boolean isLink;
		private boolean isGuid;

		private News currentNews;

		private boolean isFirstItem;

		private final List<News> news;

		public NewsXmlHandler(List<News> news) {
			this.news = news;
		}

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			if (qName.equals(ITEM)) {
				isFirstItem = true;
				currentNews = new News();
			} else if (qName.equals(TITLE)) {
				isTitle = true & isFirstItem;
			} else if (qName.equals(DESCRIPTION)) {
				isDescription = true & isFirstItem;
			} else if (qName.equals(LINK)) {
				isLink = true & isFirstItem;
			} else if (qName.equals(GUID)) {
				isGuid = true & isFirstItem;
			} else if (qName.equals(ENCLOSURE)) {
				currentNews.setImageUrl(attributes.getValue(URL));
			}
		}

		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			if (isTitle) {
				currentNews.setTitle(new String(ch, start, length));
				isTitle = false;
			} else if (isDescription) {
				currentNews.setDescription(new String(ch, start, length));
				isDescription = false;
			} else if (isLink) {
				currentNews.setLink(new String(ch, start, length));
				isLink = false;
			} else if (isGuid) {
				currentNews.setGuid(new String(ch, start, length));
				isGuid = false;
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			if (qName.equals(ITEM)) news.add(currentNews);
		}

	}
}
